package com.cncf.service.impl;

import com.cncf.dao.FirstReportViewMapper;
import com.cncf.entity.*;
import com.cncf.response.ResponseData;
import com.cncf.service.FirstReportViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zj
 */
@Service
public class FirstReportViewServiceImpl implements FirstReportViewService {
    @Autowired
    private FirstReportViewMapper firstReportViewMapper;

    @Override
    public ResponseData<FirstReportView> selectFirstReportViewById(String id) {
        ResponseData<FirstReportView> responseData = new ResponseData<>();
        FirstReportViewExample firstReportViewExample=new FirstReportViewExample();
        FirstReportViewExample.Criteria criteria=firstReportViewExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<FirstReportView> list = firstReportViewMapper.selectByExample(firstReportViewExample);
        if (list.size()==0){
            responseData.jsonFill(2,"无效的报告id",null);
            return responseData;
        }
        FirstReportView firstReportView = list.get(0);
        responseData.jsonFill(1,null,firstReportView);
        responseData.setCount(list.size());
        return responseData;
    }

    @Override
    public ResponseData<List<FirstReportView>> selectAllFirstReportViewByPage(Integer page, Integer pageSize) {
        ResponseData<List<FirstReportView>> responseData = new ResponseData<>();
        List<FirstReportView> firstReportViewList;
        if (page == null || pageSize == null) {
            FirstReportViewExample firstReportViewExample=new FirstReportViewExample();
            List<FirstReportView> list = firstReportViewMapper.selectByExample(firstReportViewExample);
            responseData.jsonFill(1, null, list);
            return responseData;
        }
        int offset = page * pageSize;
        int limit = pageSize;
        firstReportViewList = firstReportViewMapper.selectAllFirstReportViewByPage(offset, limit);
        responseData.jsonFill(1, null, firstReportViewList);
        responseData.setCount(firstReportViewList.size());
        return responseData;
    }

    @Override
    public ResponseData<List<FirstReportView>> fuzzySearch(String reportNumber, String productName, String productUnit) {
        ResponseData<List<FirstReportView>> responseData = new ResponseData<>();
        FirstReportViewExample firstReportViewExample=new FirstReportViewExample();
        FirstReportViewExample.Criteria criteria=firstReportViewExample.createCriteria();
        if (reportNumber != null) {
            //大小写转换
            String firstTwoChar=reportNumber.substring(0,2);
            if ("gn".equals(firstTwoChar)||"GN".equals(firstTwoChar)||"gN".equals(firstTwoChar)){
                reportNumber="Gn"+reportNumber.substring(2);
                System.out.println("111检测reportNumber:"+reportNumber);
            }
            String firstChar=reportNumber.substring(0,1);
            if ("N".equals(firstChar)){
                reportNumber="n"+reportNumber.substring(1);
                System.out.println("222检测reportNumber:"+reportNumber);
            }

            String searchText = "%" + reportNumber + "%";
            criteria.andReportNumberLike(searchText);
        }
        if (productName != null) {
            String searchText = "%" + productName + "%";
            criteria.andProductNameLike(searchText);
        }
        if (productUnit != null) {
            String searchText = "%" + productUnit + "%";
            criteria.andEnterpriseNameLike(searchText);
        }
        List<FirstReportView> list = firstReportViewMapper.selectByExample(firstReportViewExample);
        responseData.jsonFill(1, null, list);
        return responseData;
    }
}
